
cnvphmmcopy <- function (correctOutput, segmentOutput, chr = c(1:22, "X", "Y"), sample_name= 'cell(s)', ...) 
{
    if (is.null(segmentOutput$segs)) {
        warning("Processed segments now found, automatically processing")
        segmentOutput$segs <- processSegments(segments$segs, 
            space(correctOutput), start(correctOutput), end(correctOutput), 
            correctOutput$copy)
    }
	
	stat_offset = 0
	ylim1=c(-1, 7)
	h_line=c( (ylim1[1]+1):(ylim1[2]-1) )
	chr=as.character(chr)
    segs <- segmentOutput$segs
    correctOutput$state <- segmentOutput$state
    cols <- stateCols()
    # range <- quantile(correctOutput$copy, na.rm = TRUE, prob = c(0.01, 0.99))

	
    a <- correctOutput[chr]
	# a <- correctOutput[which(correctOutput$space %in% chr),]
    # b <- segs[which(segs$chr == chr[1]), ]
	b <- segs[which(segs$chr %in% chr), ]
	data_path=Sys.getenv('data_path')
	chr_len=paste(data_path, 'b37_chr_ln_y.RData', sep='/')
	load(chr_len)
	target_chr_len=b37_chr_ln[which(b37_chr_ln$chr %in% chr),]
	xlim=c(1, sum(as.numeric(target_chr_len$len)))
	ranges=as.data.frame(a$ranges)
	a$x=ranges$start

	pdf("1.cnv.hmmcopy.pdf", width=12, height=3)
	par(pch=18, lwd = .2, ann=F, xaxs='i', yaxs='i')
	
	plot(NA, type='n', xlim=xlim, ylim=ylim1, cex.axis=.5)
	abline(h=h_line, lty=3, col='gray')
	
	
	# plot(a$x[which(a$space==chr[1])], a$copy[which(a$space==chr[1])], col = cols[as.numeric(as.character(a$state))], xlim=xlim, ylim = range, xlab="Position on the genome(bp)", ylab="Copy number states", ...)
	main_title=paste("CNVs of sample ", sample_name)
	title(main=main_title, xlab="Position on the genome(bp)", ylab="Copy number")
	
		
	for (i in 1:nrow(target_chr_len)) {
		pre_len = sum(as.numeric(target_chr_len$len[0:(i-1)]))
		
		chr_lab_pos=pre_len + target_chr_len$len[i] %/% 2
		
		axis(3, at=chr_lab_pos, labels=target_chr_len$chr[i],tick=F, cex.axis=.5, line=-1)
		abline(v=pre_len, lty=3, col='gray')
		# lines( ( a$x[ which( a$space == chr[i] ) ] + pre_len ), a$copy[which(a$space==chr[i])], type="p", col = cols[as.numeric(as.character(a$state))] )
		
		points( ( a$x[ which( a$space == chr[i] ) ] + pre_len ), ( a$copy[which(a$space==chr[i])] + stat_offset ), col = cols[as.numeric(as.character(a$state[ which( a$space == chr[i] ) ]))], cex=.25)
		
		
		
		# lines(c(b$start[1], b$end[1]), rep(b$median[1], 2), lwd = 5, 
				# col = "green")
		# for (k in 1:nrow(b)) {
		
			# lines(c(b$start[k], b$end[k]), rep(b$median[k], 2), lwd = 5, 
				# col = "green")
		
		# }	
		
		# pre_len = sum(as.numeric(target_chr_len$len[0:(i-1)]))
		selected_chr=which( b$chr==target_chr_len$chr[i])
		medi=matrix(c(c(b$start[selected_chr], b$end[selected_chr])+pre_len, b$median[selected_chr]), ncol=3)
		
		# matrix(c( c(b$start[ which( b$chr==target_chr_len$chr[1] ) ], b$end[which(b$chr==target_chr_len$chr[1])] ) + pre_len), b$median[ which( b$chr==target_chr_len$chr[1] ) ])
		
		for (k in 1:nrow(medi)) {
			lines( c(medi[k,1], medi[k,2]), rep(medi[k,3]+ stat_offset, 2), col = "black", lwd=2 )
		}
	
	}
	
	dev.off()
}
